<?php

namespace app\admin\model;

use think\Model;

class SendOrderModel extends Model
{
    // 确定链接表名
    protected $name = 'send_order';

    
    public function memberModel(){
        return $this->belongsTo('MemberModel', 'send_uid', 'memb_id');
    }

    public function getSenderAttr($value, $data){
        return db('member')->where('memb_id', $data['sender_id'])->value('memb_phone');
    }
    
    public function getSendStatusAttr($value){
        $status = ['未处理', '进行中', '已完成', '已取消'];
        return $status[$value];
    }

    /**
     * 根据搜索条件获取订单列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getUsersByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
    }

    /**
     * 根据搜索条件获取所有的订单数量
     * @param $where
     */
    public function getAllUsers($where)
    {
        return $this->where($where)->count();
    }


    /**
     * 编辑订单信息
     * @param $param
     */
    public function editUser($param)
    {
        try{
            $result =  $this->save($param, ['id' => $param['id']]);
            return msg(1, url('sendorder/index'), '编辑用户成功');
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    /**
     * 商家接单
     * @param $id
     */
    public function ownReciver($id)
    {
        try{

            $this->where('id', $id)->update([
                'sender_id' => 1,
                'send_status' =>1
            ]);
            return msg(1, '', '接单成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }


    /**
     * 更新订单状态
     * @param array $param
     */
    public function updateStatus($param = [], $uid)
    {
        try{

            $this->where('id', $uid)->update($param);
            return msg(1, '', 'ok');
        }catch (\Exception $e){

            return msg(-1, '', $e->getMessage());
        }
    }
}
